using BackData;
using Domain;
using Domain.生产订单;
using Infratructure;
using MediatR;
using Team.API.Applocation.Command.Order;

namespace Team.API.Applocation.Hanldler.Order
{
    /// <summary>
    /// 删除订单命令处理器
    /// </summary>
    public class DeleteOrderHandler : IRequestHandler<DeleteOrderCommand, APIResult<int>>
    {
        private readonly IRepository<OrderModel> _orderRep;
        private readonly IRepository<Productionschedulemodel> _productionRep;
        private readonly ILogger<DeleteOrderHandler> _logger;

        public DeleteOrderHandler(
            IRepository<OrderModel> orderRep,
            IRepository<Productionschedulemodel> productionRep,
            ILogger<DeleteOrderHandler> logger)
        {
            _orderRep = orderRep;
            _productionRep = productionRep;
            _logger = logger;
        }

        public async Task<APIResult<int>> Handle(DeleteOrderCommand request, CancellationToken cancellationToken)
        {
            try
            {
                // 查找订单
                var order = await _orderRep.GetAll()
                    .Where(x => x.Id == request.Id && !x.IsDel)
                    .FirstAsync();

                if (order == null)
                {
                    return new APIResult<int>
                    {
                        Code = StateCode.Fail,
                        Message = "订单不存在",
                        Result = 0
                    };
                }

                // 检查是否有关联的生产排程
                var hasProduction = await _productionRep.GetAll()
                    .Where(x => x.OrderId == request.Id && !x.IsDel)
                    .AnyAsync();

                if (hasProduction)
                {
                    return new APIResult<int>
                    {
                        Code = StateCode.Fail,
                        Message = "该订单已有生产排程，无法删除",
                        Result = 0
                    };
                }

                // 软删除
                order.IsDel = true;
                var result = await _orderRep.UpdateAsync(order);

                if (result > 0)
                {
                    _logger.LogInformation($"订单删除成功: ID={request.Id}");
                    return new APIResult<int>
                    {
                        Code = StateCode.OK,
                        Message = "订单删除成功",
                        Result = result
                    };
                }
                else
                {
                    return new APIResult<int>
                    {
                        Code = StateCode.Fail,
                        Message = "订单删除失败",
                        Result = 0
                    };
                }
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "订单删除失败");
                return new APIResult<int>
                {
                    Code = StateCode.Fail,
                    Message = $"订单删除失败: {ex.Message}",
                    Result = 0
                };
            }
        }
    }
}
